package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.dao.*;
import com.baizhi.cmfz.entity.*;
import com.baizhi.cmfz.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Service
public class RoleServiceImpl implements RoleService {
  @Autowired
    private AdminDao adminDao;
    @Autowired
    private AdminResourceDao adminResourceDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleResourceDao roleResourceDao;
    @Autowired
    private ResourceDao resourceDao;

    @Override
         public Set<String> getAllRolesByUserName(String username) {
             Admin admin=adminDao.showNameAndPwd(username);
             List<AdminRole> adminRoleList=adminResourceDao.showList(admin.getId());
             Set<String> roles=new HashSet<>();
             for(AdminRole adminRole:adminRoleList){
                 Role role=roleDao.showById(adminRole.getRoleId());
                 roles.add(role.getRoleName());
             }
             return null;
         }

         @Override
         public Set<String> getAllPermissionsByUserName(String username) {
             Set<String> allRolesByUserName=getAllRolesByUserName(username);
             Set<String> permissions=new HashSet<>();
             for(String roleName:allRolesByUserName){
                 List<RoleResource> roleResources=roleResourceDao.showList(roleName);
                 for(RoleResource roleResource:roleResources){
                     System.out.println(roleResource.getResourceId());
                     Resource resource=resourceDao.showOne(roleResource.getResourceId());
                     if(resource!=null){
                         permissions.add(resource.getPermission());
                     }
                 }
             }
             return permissions;
         }

    @Override
    public Map selectRoleByPage(int page, int limit) {
        return null;
    }

    @Override
    public List<Resource> showAllResource() {
        return null;
    }
}
